package org.classg.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.classg.order.entity.Order;
import org.classg.order.service.OrderService;
import org.classg.utils.JwtUtils;
import org.classg.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-06-16
 */
@Api(description = "订单管理")
@RestController
@RequestMapping("/orderservice/order")
@CrossOrigin
public class OrderController {

    @Autowired
    private OrderService orderService;

    @ApiOperation("根据课程id和用户id创建订单，返回订单id")
    @PutMapping("/insertOrderByCourderIdAndUserId/{courseId}")
    public R insertOrderByCourderIdAndUserId(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId,
                                             @ApiParam(name = "request", value = "获取用户对象的请求", required = true) HttpServletRequest request) {
        String orderId = orderService.insertOrderByCourderIdAndUserId(courseId, JwtUtils.getMemberIdByJwtToken(request));
        return R.ok().data("orderId", orderId);
    }

    @ApiOperation("根据订单id查询订单数据")
    @GetMapping("/queryOrderByOrderNo/{orderNo}")
    public R queryOrderByOrderNo(@ApiParam(name = "orderNo", value = "订单id", required = true) @PathVariable String orderNo) {
        return R.ok().data("order", orderService.getOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNo, orderNo)));
    }

    @ApiOperation("根据用户id和课程id查询订单信息")
    @GetMapping("/queryOrderByUserIdAndCourseId/{userId}/{courseId}")
    public Boolean queryOrderByUserIdAndCourseId(@ApiParam(name = "userId", value = "用户id", required = true) @PathVariable String userId,
                                                 @ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        int count = orderService.count(new LambdaQueryWrapper<Order>().eq(Order::getMemberId, userId).eq(Order::getCourseId, courseId));
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }


}

